<?php
/**
 * Created by PhpStorm.
 * User: JX
 * Date: 2017/9/11
 * Time: 14:03
 */

namespace App\Http\Controllers\API\WeChat;


use App\Http\Controllers\Controller;
use App\Models\WechatVideoSource;
use FFMpeg\Coordinate\TimeCode;
use Illuminate\Http\Request;
use FFMpeg\FFMpeg;
class WeChatVideoController extends Controller
{
    public function index(Request $request)
    {
        $queryString = $request->get('query', '');
        $models = WechatVideoSource::query()
            ->where('videoTitle', 'like', '%' . $queryString . '%')->orderBy('id','desc')
            ->paginate(18);
        return $models;
    }

    /**
     * 添加科室
     * @param Request $request
     * @return mixed
     */
    public function store(Request $request)
    {
        $model = new WechatVideoSource();
        $model->fill($request->all());
        if ($model->save()) {
            return $model;
        } else {
            return response($model->getErrors()->toArray(), 406);
        }
    }


    /**
     * 修改员工
     * @param User $User
     * @param Request $request
     * @return mixed
     */
    public function update(Request $request)
    {
        $id = $request->get('id');
        $WechatVideoSource = $this->findModel($id);
        $WechatVideoSource->fill($request->all());
        if ($WechatVideoSource->save()) {
            return response($WechatVideoSource, 200);
        } else {
            return response($WechatVideoSource->getErrors()->toArray(), 406);
        }
    }


    /**
     * 删除
     * @param $id
     * @return mixed
     */
    public function destroy($id)
    {
        $model = $this->findModel($id);
        try {
            $model->delete();
            return ['state' => 'success'];
        } catch (\Exception $e) {
            return ['state' => 'failed', 'message' => $e->getMessage()];
        }
    }


    /**
     * 返回单个实例
     * @param User $user
     * @return mixed
     */
    public function show($id)
    {
        return $this->findModel($id);
    }




    /**
     * @param $id
     * @return Staff
     */
    public function findModel($id)
    {
        $model = WechatVideoSource::findOrFail($id);
        return $model;
    }
}